<?php
function getSysnavModuleID($id){
    $result =D('Sysnav')->getField('moduleID',"id=$id");
    return $result;
}

function getSysnavName($id){
    $result =D('Sysnav')->getField('name',"id=$id");
    return $result;
}

function getSysmoduleName($id){
    $result =D('Sysmodule')->getField('name',"id=$id");
    return $result;
}

function getCategoryName($id){
    $result =D('Category')->getField('name',"id=$id");
    return $result;
}

function getSysadmingroupName($id){
    $result =D('Sysadmingroup')->getField('name',"id=$id");
    return $result;
}


/** 
* friendlyDate() 
* 
* @param mixed $sTime 
* @param string $type,当full时,返回全部时间日期 
* @return 友好的时间日期 
*/ 
function fDate($sTime,$type = 'normal') { 
    //sTime=源时间，cTime=当前时间，dTime=时间差
    $cTime        =    time();
    $dTime        =    $cTime - $sTime;
    $dDay        =    intval(date("Ymd",$cTime)) - intval(date("Ymd",$sTime));
    $dYear        =    intval(date("Y",$cTime)) - intval(date("Y",$sTime));
    //normal：n秒前，n分钟前，n小时前，日期
    if($type=='normal'){
        if( $dTime < 60 ){
            echo $dTime."秒前";
        }elseif( $dTime < 3600 ){
            echo intval($dTime/60)."分钟前";
        }elseif( $dTime >= 3600 && $dDay == 0  ){
            echo intval($dTime/3600)."小时前";
        }elseif($dDay !=0 && $dYear==0){
            echo intval($dDay)."天前";
        }else{
            echo date("Y年m月",$sTime);
        }
        //full: Y-m-d , H:i:s
    }elseif($type=='full'){
        echo date("Y-m-d , H:i:s",$sTime);
    }else{
        if( $dTime < 60 ){
            return $dTime."秒前";
        }elseif( $dTime < 3600 ){
            return intval($dTime/60)."分钟前";
        }elseif( $dTime >= 3600 && $dDay == 0  ){
            return intval($dTime/3600)."小时前";
        }elseif($dYear==0){
            return date("m-d ,H:i",$sTime);
        }else{
            return date("Y-m-d ,H:i",$sTime);
        }
    }
}
function toDate($time=null, $format = 'Y-m-d H:i:s')
{
    if (empty($time)) {
        $time = time();
    }
    $format = str_replace('#', ':', $format);
    return date(auto_charset($format), $time);
}

function sDate($time)
{
    if (empty($time)) {
        $time = time();
    }
    return date("m月d日 ,H:i", $time);
}

function getExamUrl(){
    $Exam_URL = __APP__."/exam/examlist/examState/0";
    if(Cookie::get("examUrl")){
        $Exam_URL = Cookie::get('examUrl');
    }
    return $Exam_URL;
}
//获取头像
function getFacePath($uid,$size){
    $face	= 	$uid."face_$size.jpg";
    if(!file_exists('./public/FaceIcons/'.$face)){
        $face = "face_$size.jpg";
    }
    return $face;
}



//检查并创建多级目录
function checkDir($path){
    $pathArray = explode('/',$path);
    $nowPath = '';
    array_pop($pathArray);
    foreach ($pathArray as $key=>$value){
        if ( ''==$value ){
            unset($pathArray[$key]);
        }else{
            if ( $key == 0 )
            $nowPath .= $value;
            else
            $nowPath .= '/'.$value;
            if ( !is_dir($nowPath) ){
                if ( !mkdir($nowPath, 0777) ) return false;
            }
        }
    }
    return true;
}


function matchImages($content) {
    $src	=	array();
    preg_match_all('/<img.*src=(.*)\\s.*>/iU',$content,$src);
    if(count($src[1])>0){
        foreach($src[1] as $v){
            $image	=	str_replace("'",'',$v);
            $image	=	str_replace('"','',$v);
            $images[] =	$image;
        }
        return $images;
    }else{
        return false;
    }

}


function matchImage($content){
    $images	 = matchImages($content);
    if(false===$images){
        $retrun		=	false;
    }else{
        $retrun		=	$images[0];
    }
    return $retrun;
}

function Cut($Str, $Length) // $Str为截取字符串，$Length为需要截取的长度
{
    global $s;
    $i = 0;
    $l = 0;
    $ll = strlen($Str);
    $s = $Str;
    $f = true;

    while ($i <= $ll) {
        if (ord($Str{$i}) < 0x80) {
            $l++;
            $i++;
        } else if (ord($Str{$i}) < 0xe0) {
            $l++;
            $i += 2;
        } else if (ord($Str{$i}) < 0xf0) {
            $l += 2;
            $i += 3;
        } else if (ord($Str{$i}) < 0xf8) {
            $l += 1;
            $i += 4;
        } else if (ord($Str{$i}) < 0xfc) {
            $l += 1;
            $i += 5;
        } else if (ord($Str{$i}) < 0xfe) {
            $l += 1;
            $i += 6;
        }

        if (($l >= $Length - 1) && $f) {
            $s = substr($Str, 0, $i);
            $f = false;
        }

        if (($l > $Length) && ($i < $ll)) {
            $s = $s . '...';
            break; //如果进行了截取，字符串末尾加省略符号“...”
        }
    }
    return $s;
}
//输出安全的html
function h($text){
    $text	=	trim($text);
    $text	=	stripslashes($text);
    //完全过滤注释
    $text	=	preg_replace('/<!--?.*-->/','',$text);
    //完全过滤动态代码
    $text	=	preg_replace('/<\?|\?>/','',$text);
    //完全过滤js
    $text	=	preg_replace('/<script?.*\/script>/','',$text);

    $text	=	str_replace('[','&#091;',$text);
    $text	=	str_replace(']','&#093;',$text);
    $text	=	str_replace('|','&#124;',$text);
    //过滤换行符
    $text	=	preg_replace('/\r?\n/','',$text);
    //br
    $text	=	preg_replace('/<br(\s\/)?>/i','[br]',$text);
    $text	=	preg_replace('/(\[br\]\s*){10,}/i','[br]',$text);
    //hr img area input
    $text	=	preg_replace('/<(hr|img|input|area|isindex)( [^><\[\]]*)>/i','[\1\2]',$text);
    //过滤多余html
    $text	=	preg_replace('/<\/?(html|head|meta|link|base|body|title|style|script|form|iframe|frame|frameset)[^><]*>/i','',$text);
    //过滤on事件lang js
    while(preg_match('/(<[^><]+)( lang|onfinish|onmouse|onexit|onerror|onclick|onkey|onload|onchange|onfocus|onblur)[^><]+/i',$text,$mat)){
        $text=str_replace($mat[0],$mat[1],$text);
    }
    while(preg_match('/(<[^><]+)(window\.|javascript:|js:|about:|file:|document\.|vbs:|cookie)([^><]*)/i',$text,$mat)){
        $text=str_replace($mat[0],$mat[1].$mat[3],$text);
    }
    //过滤合法的html标签
    while(preg_match('/<([a-z]+)[^><\[\]]*>[^><]*<\/\1>/i',$text,$mat)){
        $text=str_replace($mat[0],str_replace('>',']',str_replace('<','[',$mat[0])),$text);
    }
    //转换引号
    while(preg_match('/(\[[^\[\]]*=\s*)(\"|\')([^\2=\[\]]+)\2([^\[\]]*\])/i',$text,$mat)){
        $text=str_replace($mat[0],$mat[1].'|'.$mat[3].'|'.$mat[4],$text);
    }
    //过滤错误的单个引号
    while(preg_match('/\[[^\[\]]*(\"|\')[^\[\]]*\]/i',$text,$mat)){
        $text=str_replace($mat[0],str_replace($mat[1],'',$mat[0]),$text);
    }
    //转换其它所有不合法的 < >
    $text	=	str_replace('<','&lt;',$text);
    $text	=	str_replace('>','&gt;',$text);
    $text	=	str_replace('"','&quot;',$text);
    //反转换
    $text	=	str_replace('[','<',$text);
    $text	=	str_replace(']','>',$text);
    $text	=	str_replace('|','"',$text);
    //过滤多余空格
    $text	=	str_replace('  ',' ',$text);
    return $text;
}

//过滤脚本代码
function cleanJs($text){
    $text	=	trim($text);
    $text	=	stripslashes($text);
    //完全过滤动态代码
    $text	=	preg_replace('/<\?|\?>/','',$text);
    //完全过滤js
    $text	=	preg_replace('/<script?.*\/script>/','',$text);
    //过滤多余html
    $text	=	preg_replace('/<\/?(html|head|meta|link|base|body|title|style|script|form|iframe|frame|frameset)[^><]*>/i','',$text);
    //过滤on事件lang js
    while(preg_match('/(<[^><]+)(lang|onfinish|onmouse|onexit|onerror|onclick|onkey|onload|onchange|onfocus|onblur)[^><]+/i',$text,$mat)){
        $text=str_replace($mat[0],$mat[1],$text);
    }
    while(preg_match('/(<[^><]+)(window\.|javascript:|js:|about:|file:|document\.|vbs:|cookie)([^><]*)/i',$text,$mat)){
        $text=str_replace($mat[0],$mat[1].$mat[3],$text);
    }
    return $text;
}
//纯文本输出
function t($text){
    $text	=	h($text);
    $text	=	strip_tags($text);
    return $text;
}

//数据转换
//获取用户名
function getNewscateName($id){
    $info	=	getCateInfo($id);
    return $info['NewsCateTitle'];
}


//获取用信息
function getCateInfo($id){
    $Newscateinfo	=	unserialize(S('Newscateinfo_'.$id));
    if($Newscateinfo){
        return $Newscateinfo;
    }else{
        $Newscateinfo	=	D('Newscate')->find($uid);
        //dump($Newscateinfo);
        S('Newscateinfo_'.$id,serialize($Newscateinfo));
        return $Newscateinfo;
    }
}


//获取用户名
function getUserName($uid){
    if($uid==0){
        return "佚名";
    }else{
        $info	=	getUserInfo($uid);
        return $info["bname"];
    }
}


//获取用信息
function getUserInfo($uid){
    $userinfo	=	unserialize(S('userinfo_'.$uid));
    if($userinfo){
        return $userinfo;
    }else{
        $userinfo	=	D('User')->find($uid);
        S('userinfo_'.$uid,serialize($userinfo));
        return $userinfo;
    }
}
include './Public/ugs.func.php';
?>
